package com.zhijian.medical.service.impl;

import com.google.common.collect.Lists;
import com.zhijian.medical.dao.local.ExperimentProcessResultDetailMapper;
import com.zhijian.medical.entity.po.ExperimentProcessResultDetail;
import com.zhijian.medical.entity.vo.request.ExperimentProcessResultDetailSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessResultDetailVo;
import com.zhijian.medical.service.ExperimentProcessResultDetailService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 实验流程-结果明细业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-04-08 23:19:31
 */
@Slf4j
@Service
public class ExperimentProcessResultDetailServiceImpl implements ExperimentProcessResultDetailService {

    @Autowired
    private ExperimentProcessResultDetailMapper mapper;

    @Override
    public ExperimentProcessResultDetail selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExperimentProcessResultDetailVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByExperimentProcessId(Long experimentProcessId) {
        mapper.deleteByExperimentProcessId(experimentProcessId);
    }

    @Override
    public Long insert(ExperimentProcessResultDetail experimentProcessResultDetail) {
        Date now = new Date();
        experimentProcessResultDetail.setCreateTime(now);
        experimentProcessResultDetail.setUpdateTime(now);
        return mapper.insert(experimentProcessResultDetail) > 0 ? experimentProcessResultDetail.getId() : 0L;
    }

    @Override
    public boolean update(ExperimentProcessResultDetail experimentProcessResultDetail) {
        Date now = new Date();
        experimentProcessResultDetail.setUpdateTime(now);
        return mapper.updateByPrimaryKey(experimentProcessResultDetail) != 0;
    }

    @Override
    public Integer count(ExperimentProcessResultDetailSearchVo experimentProcessResultDetailSearchVo) {
        return mapper.count(experimentProcessResultDetailSearchVo);
    }

    @Override
    public List<ExperimentProcessResultDetailVo> selectList(ExperimentProcessResultDetailSearchVo experimentProcessResultDetailSearchVo) {
        return mapper.selectList(experimentProcessResultDetailSearchVo);
    }

    @Override
    public Page<ExperimentProcessResultDetailVo> selectPage(ExperimentProcessResultDetailSearchVo experimentProcessResultDetailSearchVo) {
        //先直接查总数
        int count = count(experimentProcessResultDetailSearchVo);
        //分页
        Page<ExperimentProcessResultDetailVo> page = new Page<>(true, count, experimentProcessResultDetailSearchVo.getPageNum(), experimentProcessResultDetailSearchVo.getPageSize());
        experimentProcessResultDetailSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExperimentProcessResultDetailVo> responseVos = this.selectList(experimentProcessResultDetailSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public void deleteByResultIds(List<Long> resultIds) {
        if (CollectionUtils.isEmpty(resultIds)) {
            resultIds = Lists.newArrayList(0L);
        }
        mapper.deleteByResultIds(resultIds);
    }

}